const path = require('path');
const sharp = require('sharp');

function copyAndResizeImage(sourcePath, destinationPath, width, height) {
  sharp(sourcePath)
    .resize(width, height)
    .toFile(destinationPath, (err, info) => {
      if (err) {
        console.error(`Copy and resize image failure ${sourcePath}:`, err);
      }
    });
}

const androidIconDir = path.join(__dirname, '..', 'android', 'app', 'src', 'main', 'res');
const androidIconList = [
  {
    filePath: path.join(androidIconDir, 'mipmap-mdpi', 'ic_launcher.png'),
    width: 48,
    height: 48
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-mdpi', 'ic_launcher_round.png'),
    width: 48,
    height: 48
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-mdpi', 'ic_launcher_foreground.png'),
    width: 108,
    height: 108
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-hdpi', 'ic_launcher.png'),
    width: 49,
    height: 49
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-hdpi', 'ic_launcher_round.png'),
    width: 49,
    height: 49
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-hdpi', 'ic_launcher_foreground.png'),
    width: 162,
    height: 162
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xhdpi', 'ic_launcher.png'),
    width: 96,
    height: 96
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xhdpi', 'ic_launcher_round.png'),
    width: 96,
    height: 96
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xhdpi', 'ic_launcher_foreground.png'),
    width: 216,
    height: 216
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxhdpi', 'ic_launcher.png'),
    width: 144,
    height: 144
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxhdpi', 'ic_launcher_round.png'),
    width: 144,
    height: 144
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxhdpi', 'ic_launcher_foreground.png'),
    width: 324,
    height: 324
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxxhdpi', 'ic_launcher.png'),
    width: 192,
    height: 192
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxxhdpi', 'ic_launcher_round.png'),
    width: 192,
    height: 192
  },
  {
    filePath: path.join(androidIconDir, 'mipmap-xxxhdpi', 'ic_launcher_foreground.png'),
    width: 432,
    height: 432
  },
];

const iosIconDir = path.join(__dirname, '..', 'ios', 'Runner', 'Assets.xcassets', 'AppIcon.appiconset');
const iosIconFileList = [
  {
    filePath: path.join(iosIconDir, 'Icon-App-20x20@2x.png'),
    width: 40,
    height: 40
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-20x20@3x.png'),
    width: 60,
    height: 60
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-20x20@1x.png'),
    width: 20,
    height: 20
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-29x29@1x.png'),
    width: 29,
    height: 29
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-29x29@2x.png'),
    width: 58,
    height: 58
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-29x29@3x.png'),
    width: 87,
    height: 87
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-40x40@2x.png'),
    width: 80,
    height: 80
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-40x40@3x.png'),
    width: 120,
    height: 120
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-40x40@1x.png'),
    width: 40,
    height: 40
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-60x60@2x.png'),
    width: 120,
    height: 120
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-60x60@3x.png'),
    width: 180,
    height: 180
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-76x76@1x.png'),
    width: 76,
    height: 76
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-76x76@2x.png'),
    width: 152,
    height: 152
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-83.5x83.5@2x.png'),
    width: 166,
    height: 166
  },
  {
    filePath: path.join(iosIconDir, 'Icon-App-1024x1024@1x.png'),
    width: 1024,
    height: 1024
  }
];

const macosIconDir = path.join(__dirname, '..', 'macos', 'Runner', 'Assets.xcassets', 'AppIcon.appiconset');
const macosIconFileList = [
  {
    filePath: path.join(macosIconDir, 'app_icon_16.png'),
    width: 16,
    height: 16
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_32.png'),
    width: 32,
    height: 32
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_64.png'),
    width: 64,
    height: 64
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_128.png'),
    width: 128,
    height: 128
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_256.png'),
    width: 256,
    height: 256
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_512.png'),
    width: 512,
    height: 512
  },
  {
    filePath: path.join(macosIconDir, 'app_icon_1024.png'),
    width: 1024,
    height: 1024
  },
];

const webDir = path.join(__dirname, '..', 'web');
const webIconDir = path.join(webDir, 'icons');
const webIconList = [
  {
    filePath: path.join(webDir, 'favicon.png'),
    width: 32,
    height: 32
  },
  {
    filePath: path.join(webIconDir, 'Icon-192.png'),
    width: 192,
    height: 192
  },
  {
    filePath: path.join(webIconDir, 'Icon-512.png'),
    width: 512,
    height: 512
  },
  {
    filePath: path.join(webIconDir, 'Icon-maskable-192.png'),
    width: 192,
    height: 192
  },
  {
    filePath: path.join(webIconDir, 'Icon-maskable-512.png'),
    width: 512,
    height: 512
  }
];

const appIconPath = path.join(__dirname, '..', 'app-icon.png');

const iconMapList = [
  ...androidIconList,
  ...iosIconFileList,
  ...macosIconFileList,
  ...webIconList
];

iconMapList.forEach(({ filePath, width, height }) => {
  copyAndResizeImage(appIconPath, filePath, width, height);
});

console.log('Icons handle successfully.');